// miniprogram/pages/ShopAdmin/MyShop/MyShop.js
const db=wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    one_1:0,
    one_2:0,
    one_3:0,
    two_1:'',
    two_2:'',
    two_3:'',
    number:0,

    leftHight: 0,
    rightHight: 0,
    GoodsLeftList: [],
    GoodsRightList: [],
    name:'',
    userimg:''
  },
  return:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  commodity:function(e){
    console.log(e.currentTarget.dataset.id)
    console.log(e.currentTarget.id)
    var that=this
    wx.cloud.callFunction({
      name: "Browse",
      data: {
        liulan: parseInt(e.currentTarget.id),
        id:e.currentTarget.dataset.id,
      }
    }).then(res => {
      console.log(res.data);
    }).catch(err => {
      console.log(err);
    })

    wx.navigateTo({
      url: "/pages/commodity/commodity?commodity=" + e.currentTarget.dataset.id + "",
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getGoods(options.openid)
    this.getEvaluate(options.openid)
  },
  getGoods: function (openid) {
    var that = this
    wx.showLoading({
      title: '加载中',
      mask: true,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
    db.collection('Goods')
    .where({
      _openid: openid,
    }).get()
    .then(res => {
      // res.data 包含该记录的数据
      console.log(res.data)
      that.setData({
        name:res.data[0].nickName,
        userimg:res.data[0].avatarUrl,
      })
      if (res.data.length == 0) {
        wx.showToast({
          title: "已无更多商品",
          icon: "none"
        })
      }
      var leftH = that.data.leftHight;
      var rightH = that.data.rightHight;
      let L = 0;
      let R = 0;
      console.log(leftH)
      console.log(rightH)
      console.log(L)
      console.log(R)
      for (let i = 0; i < res.data.length; i++) {
        wx.getImageInfo({
          src: res.data[i].fileIDs[0],
          success: function (res2) {
            console.log(res2.height)
            console.log(res2.width)
            var height = parseInt(Math.round(res2.height * 345 / res2.width))
            console.log(height)
            if (leftH == rightH || leftH < rightH) {
              leftH += res2.height;
              var id = "GoodsLeftList[" + L + "].id"
              var title = "GoodsLeftList[" + L + "].title"
              var color = "GoodsLeftList[" + L + "].color"
              var fileid = "GoodsLeftList[" + L + "].fileid"
              var numbers = "GoodsLeftList[" + L + "].numbers"
              var rmb = "GoodsLeftList[" + L + "].rmb"
              var size = "GoodsLeftList[" + L + "].size"
              var state = "GoodsLeftList[" + L + "].state"
              var show = "GoodsLeftList[" + L + "].show"
              var sale = "GoodsLeftList[" + L + "].sale"
              var height = "GoodsLeftList[" + L + "].height"
              var liulan = "GoodsLeftList[" + L + "].liulan"
              that.setData({
                [title]: res.data[i].title,
                [id]: res.data[i]._id,
                [fileid]: res.data[i].fileIDs[0],
                [rmb]: res.data[i].rmb,
                [state]: res.data[i].state,
                [size]: res.data[i].size,
                [numbers]: res.data[i].numbers,
                [color]: res.data[i].color,
                [show]: false,
                [sale]: res.data[i].sale,
                [liulan]: res.data[i].liulan,
                [height]: height + "rpx",
                leftHight: leftH,
              })
              console.log("lalalala")
              L = L + 1
              console.log(that.data.GoodsLeftList)
            } else {
              rightH += res2.height;
              var id = "GoodsRightList[" + R + "].id"
              var title = "GoodsRightList[" + R + "].title"
              var color = "GoodsRightList[" + R + "].color"
              var fileid = "GoodsRightList[" + R + "].fileid"
              var numbers = "GoodsRightList[" + R + "].numbers"
              var rmb = "GoodsRightList[" + R + "].rmb"
              var size = "GoodsRightList[" + R + "].size"
              var state = "GoodsRightList[" + R + "].state"
              var show = "GoodsRightList[" + R + "].show"
              var sale = "GoodsRightList[" + R + "].sale"
              var height = "GoodsRightList[" + R + "].height"
              var liulan = "GoodsRightList[" + R + "].liulan"
              that.setData({
                [title]: res.data[i].title,
                [id]: res.data[i]._id,
                [fileid]: res.data[i].fileIDs[0],
                [rmb]: res.data[i].rmb,
                [state]: res.data[i].state,
                [size]: res.data[i].size,
                [numbers]: res.data[i].numbers,
                [color]: res.data[i].color,
                [show]: false,
                [sale]: res.data[i].sale,
                [liulan]: res.data[i].liulan,
                [height]: height + "rpx",
                rightHight: rightH,
              })
              R = R + 1
              console.log(that.data.GoodsRightList)
            }
            if (i == res.data.length - 1) {
              wx.hideLoading();
            }
          }
        })
      }

    })
    .catch(err => {
      console.error(err)
      wx.hideLoading();
    })
  },
  getEvaluate:function(openid){
    var that=this
    db.collection('Evaluate').where({
      SellOpenid:openid
    }).get().then(res => {
      // res.data 包含该记录的数据
      console.log(res.data)
      var one_1=0
      var one_2=0
      var one_3=0
      for(let i=0;i<res.data.length;i++){
        one_1= one_1+res.data[i].one_1
        one_2= one_2+res.data[i].one_2
        one_3= one_3+res.data[i].one_3
      }
      if(res.data.length!=0){
        one_1=(one_1*1.0/res.data.length).toFixed(1)
        one_2=(one_2*1.0/res.data.length).toFixed(1)
        one_3=(one_3*1.0/res.data.length).toFixed(1)
      }
      

      var two_1='低'
      var two_2='低'
      var two_3='低'
      if(one_1>3.5){
        two_1='高'
      }else if(one_1>2){
        two_1='中'
      }
      if(one_2>3.5){
        two_2='高'
      }else if(one_2>2){
        two_2='中'
      }
      if(one_3>3.5){
        two_3='高'
      }else if(one_3>2){
        two_3='中'
      }
      that.setData({
        one_1:one_1,
        one_2:one_2,
        one_3:one_3,
        two_1:two_1,
        two_2:two_2,
        two_3:two_3,
      })
    })
    db.collection('Goods').where({
      _openid:openid
    }).get().then(res2 => {
      // res.data 包含该记录的数据
      console.log(res2.data.length)
      that.setData({
        number:res2.data.length
      })
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})